package com.piaohailin.right;

/**
 * 1 2 4 8 数值权限点校验思路
 * 数值越大代表拥有的权限越大
 *
 * @author piaohailin@163.com
 * @date 2021/4/27
 */
public class Numerical {
    private static int NONE = 0;
    private static int USER = 1;
    private static int USER_ADMIN = 2;
    private static int ADMIN = 4;

    private int userRole = 4;

    /**
     * 权限点设计原则，前面点数值相加不能大于n-1位点数值。例如：1+2 < 4
     */
    public void compare() {
        if (userRole >= USER)
            System.out.println("拥有权限");
        else
            System.out.println("没有权限");
    }

    /**
     * 输入权限数值，分解拥有几个权限点
     * 用与运算分解
     * @param right
     */
    public void resolve(int right) {
        if ((right & ADMIN) == ADMIN)
            System.out.println("拥有权限 ADMIN");
        if ((right & USER_ADMIN) == USER_ADMIN)
            System.out.println("拥有权限 USER_ADMIN");
        if ((right & USER) == USER)
            System.out.println("拥有权限 USER");
//        if ((right & NONE) == NONE)
//            System.out.println("拥有权限 NONE");

        System.out.println("---------------");
    }

    public static void main(String[] args) {
        Numerical obj = new Numerical();
//        obj.compare();
        obj.resolve(7);
        obj.resolve(6);
        obj.resolve(5);
        obj.resolve(3);
        obj.resolve(2);

//        System.out.println(5 & 4);
//        System.out.println(5 & 2);
//        System.out.println(5 & 1);
//        System.out.println(5 & 0);

    }
}
